package cloud.estate.chargeDetail.service.impl;

import cloud.estate.chargeDetail.service.ChargeDetailService;
import cloud.estate.commons.bean.ChargeDetail;
import cloud.estate.commons.handler.exception.AddOneException;
import cloud.estate.commons.handler.exception.DealOneByIdException;
import cloud.estate.commons.handler.exception.FindOneException;
import cloud.estate.commons.handler.exception.UpdateOneException;
import cloud.estate.commons.mapper.ChargeDetailMapper;
import cloud.estate.commons.util.JsonResult;
import cloud.estate.commons.util.ResultTool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ChargeDetailServiceImpl extends ServiceImpl<ChargeDetailMapper, ChargeDetail> implements ChargeDetailService {
    @Override
    public JsonResult addOneChargeDetail(ChargeDetail chargeDetail) {
        try {
            boolean save = save(chargeDetail);
            if (save) {
                return ResultTool.success("添加成功");
            }
            return ResultTool.fail(501, "添加失败");
        } catch (Exception e) {
            throw new AddOneException(e.getMessage());
        }
    }

    @Override
    public JsonResult deleteOneChargeDetail(String id) {
        try {
            boolean removeById = removeById(id);
            if (removeById) {
                return ResultTool.success("删除成功");
            }
            return ResultTool.fail(502, "删除失败");
        } catch (Exception e) {
            throw new DealOneByIdException(e.getMessage());
        }
    }

    @Override
    public JsonResult updateOneChargeDetail(ChargeDetail chargeDetail) {
        try {
            boolean updateById = updateById(chargeDetail);
            if (updateById) {
                return ResultTool.success("修改成功");
            }
            return ResultTool.fail(503, "修改失败");
        } catch (Exception e) {
            throw new UpdateOneException(e.getMessage());
        }
    }

    @Override
    public JsonResult findOneChargeDetail(String id) {
        try {
            ChargeDetail chargeDetail = getById(id);
            if (chargeDetail != null) {
                return ResultTool.success(chargeDetail);
            }
            return ResultTool.fail(504, "查询失败");
        } catch (Exception e) {
            throw new FindOneException(e.getMessage());
        }
    }

    @Override
    public JsonResult findAllChargeDetail() {
        try {
            List<ChargeDetail> list = list();
            if (list != null) {
                return ResultTool.success(list);
            }
            return ResultTool.fail(505, "查询失败");
        } catch (Exception e) {
            throw new FindOneException(e.getMessage());
        }
    }
}
